﻿//-----------------------------------------------------------------------
// <copyright file="SetEntityMatrixAction.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Scene.Action
{
    using System;
    using System.Collections.Generic;
    using TileTool.Action;
    using TileTool.Common.Math;
    using TileTool.Log;
    using TileTool.Scene.Entity;

    /// <summary>
    /// Action to set the matrix of entities.
    /// </summary>
    public class SetEntityMatrixAction : SceneAction
    {
        #region Constants - Private

        /// <summary>
        /// Log message group.
        /// </summary>
        private const string LogGroup = "SetEntityMatrixAction";

        #endregion

        #region Fields - Private

        /// <summary>
        /// Dictionary which tracks the entities we will set the matrix of.
        /// </summary>
        private IDictionary<IEntity, MatrixData> entityDictionary = new Dictionary<IEntity, MatrixData>();

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor for the action.
        /// </summary>
        /// <param name="manager">Scene this action operates on.</param>
        public SetEntityMatrixAction(ISceneManager manager) 
            : base(manager)
        {
            this.Valid = false;
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Add an entity to the action so its matrix will be set during a do / undo.
        /// </summary>
        /// <param name="entity">Entity to set the matrix on.</param>
        /// <param name="start">Start matrix.</param>
        /// <param name="end">End matrix.</param>
        public void AddEntity(IEntity entity, Matrix2D start, Matrix2D end)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity", "Entity must be non-null.");
            }

            if (entity.Scene != this.Scene)
            {
                throw new InvalidOperationException("Entity scene does not match scene for action.");
            }

            this.entityDictionary.Remove(entity);
            this.entityDictionary[entity] = new MatrixData(start, end);
            this.Valid = true;
        }

        /// <summary>
        /// Do the action.
        /// </summary>
        public override void Do()
        {
            if (!this.Valid)
            {
                return;
            }

            LogSystem.LogInfo(LogGroup, "Do() - Setting matrix for " + this.entityDictionary.Count + " entities.");

            IEntityCollection collection = new EntityCollection(this.entityDictionary.Keys);

            foreach (KeyValuePair<IEntity, MatrixData> item in this.entityDictionary)
            {
                IEntity entity = item.Key;
                MatrixData data = item.Value;

                entity.Matrix = data.End;
            }
        }

        /// <summary>
        /// Undo the action.
        /// </summary>
        public override void Undo()
        {
            if (!this.Valid)
            {
                return;
            }

            LogSystem.LogInfo(LogGroup, "Undo() - Setting matrix for " + this.entityDictionary.Count + " entities.");

            IEntityCollection collection = new EntityCollection(this.entityDictionary.Keys);

            foreach (KeyValuePair<IEntity, MatrixData> item in this.entityDictionary)
            {
                IEntity entity = item.Key;
                MatrixData data = item.Value;

                entity.Matrix = data.Start;
            }
        }

        #endregion

        #region Class - Private

        /// <summary>
        /// Class which wraps matrix data for an entity.
        /// </summary>
        private class MatrixData
        {
            /// <summary>
            /// Constructor for the data class.
            /// </summary>
            /// <param name="start">Start matrix.</param>
            /// <param name="end">End matrix.</param>
            public MatrixData(Matrix2D start, Matrix2D end)
            {
                this.Start = start;
                this.End = end;
            }

            /// <summary>
            /// Gets the start matrix.
            /// </summary>
            public Matrix2D Start
            {
                get;
                private set;
            }

            /// <summary>
            /// Gets the end matrix.
            /// </summary>
            public Matrix2D End
            {
                get;
                private set;
            }
        }

        #endregion
    }
}
